<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
<meta name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

    <meta name="author" content="WD">





<title>python学习笔记(二) | WD&#39;s blog</title>



    <link rel="icon" href="/favicon1.ico">




    <!-- stylesheets list from _config.yml -->
    
    <link rel="stylesheet" href="/css/style.css">
    



    <!-- scripts list from _config.yml -->
    
    <script src="/js/script.js"></script>
    
    <script src="/js/tocbot.min.js"></script>
    
    <script src="/js/snow.js"></script>
    



    
    
        
    


<meta name="generator" content="Hexo 5.4.0"></head>
<body>
    <div class="wrapper">
        <header>
    <nav class="navbar">
        <div class="container">
            <div class="navbar-header header-logo"><a href="/">WD&#39;s Blog</a></div>
            <div class="menu navbar-right">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
                <input id="switch_default" type="checkbox" class="switch_default">
                <label for="switch_default" class="toggleBtn"></label>
            </div>
        </div>
    </nav>

    
    <nav class="navbar-mobile" id="nav-mobile">
        <div class="container">
            <div class="navbar-header">
                <div>
                    <a href="/">WD&#39;s Blog</a><a id="mobile-toggle-theme">·&nbsp;Light</a>
                </div>
                <div class="menu-toggle" onclick="mobileBtn()">&#9776; Menu</div>
            </div>
            <div class="menu" id="mobile-menu">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
            </div>
        </div>
    </nav>

</header>
<script>
    var mobileBtn = function f() {
        var toggleMenu = document.getElementsByClassName("menu-toggle")[0];
        var mobileMenu = document.getElementById("mobile-menu");
        if(toggleMenu.classList.contains("active")){
           toggleMenu.classList.remove("active")
            mobileMenu.classList.remove("active")
        }else{
            toggleMenu.classList.add("active")
            mobileMenu.classList.add("active")
        }
    }
</script>
        <div class="main">
            <div class="container">
    
    
        <div class="post-toc">
    <div class="tocbot-list">
    </div>
    <div class="tocbot-list-menu">
        <a class="tocbot-toc-expand" onclick="expand_toc()">Expand all</a>
        <a onclick="go_top()">Back to top</a>
        <a onclick="go_bottom()">Go to bottom</a>
    </div>
</div>

<script>
    document.ready(
        function () {
            tocbot.init({
                tocSelector: '.tocbot-list',
                contentSelector: '.post-content',
                headingSelector: 'h1, h2, h3, h4, h5',
                collapseDepth: 1,
                orderedList: false,
                scrollSmooth: true,
            })
        }
    )

    function expand_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 6,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "collapse_toc()");
        b.innerHTML = "Collapse all"
    }

    function collapse_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 1,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "expand_toc()");
        b.innerHTML = "Expand all"
    }

    function go_top() {
        window.scrollTo(0, 0);
    }

    function go_bottom() {
        window.scrollTo(0, document.body.scrollHeight);
    }

</script>
    

    
    <article class="post-wrap">
        <header class="post-header">
            <h1 class="post-title">python学习笔记(二)</h1>
            
                <div class="post-meta">
                    
                        Author: <a itemprop="author" rel="author" href="/about/">WD</a>
                     &nbsp;

                    
                        <span class="post-time">
                        Date: <a href="#">February 7, 2019&nbsp;&nbsp;11:14:31</a>
                        </span>
                     &nbsp;
                    
                        <span class="post-category">
                    Category:
                            
                                <a href="/categories/Python/">Python</a>
                            
                        </span>
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <br>
    <span id="busuanzi_container_site_pv">总阅读量:<a href="#"><span id="busuanzi_value_page_pv"></span></a>次</span>&nbsp;
    <span class="post-count">文章字数:<a href="#">5.4k</span></a>&nbsp;
     <span class="post-count">阅读时长:<a href="#">28</span>min</a>
                    
                </div>
            
        </header>

        <div class="post-content">
            <h2 id="1-列表"><a href="#1-列表" class="headerlink" title="1.列表"></a>1.列表</h2><blockquote>
<ul>
<li>列表是python的内置可变序列，是包含若干元素的有序连续内存空间，用[ ]表示，列表可以和元组，字典等嵌套使用，可以增加删除元素，列表对象自动进行内存的扩展与收缩<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line">a=[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>]</span><br><span class="line">a.append(<span class="number">6</span>) <span class="comment">#将元素x添加到列表尾部</span></span><br><span class="line"><span class="built_in">print</span>(a)</span><br><span class="line">a.extend(a) <span class="comment">#将列表L中所有元素添加至列表尾部</span></span><br><span class="line"><span class="built_in">print</span>(a)</span><br><span class="line">a.insert(<span class="number">2</span>,<span class="number">0</span>) <span class="comment">#在列表指定位置添加元素x，第一个是位置</span></span><br><span class="line"><span class="built_in">print</span>(a)</span><br><span class="line">a.remove(<span class="number">0</span>) <span class="comment">#删除列表中首次出现的指定元素x</span></span><br><span class="line"><span class="built_in">print</span>(a)</span><br><span class="line">a.pop() <span class="comment">#删除并返回列表指定位置的元素，默认最后一个元素</span></span><br><span class="line"><span class="built_in">print</span>(a)</span><br><span class="line"><span class="built_in">print</span>(a.index(<span class="number">1</span>)) <span class="comment">#返回第一个值为x的元素下表,若不存在则抛出异常</span></span><br><span class="line"><span class="built_in">print</span>(a.count(<span class="number">6</span>)) <span class="comment">#返回指定元素x在列表中出现的次数</span></span><br><span class="line">a.reverse() <span class="comment">#对列表进行反转颠倒(前后移一颠倒)</span></span><br><span class="line"><span class="built_in">print</span>(a)</span><br><span class="line">a.sort() <span class="comment">#对列表进行排序</span></span><br><span class="line"><span class="built_in">print</span>(a)</span><br><span class="line">b=a.copy() <span class="comment">#返回列表的浅复制</span></span><br><span class="line"><span class="built_in">print</span>(b)</span><br><span class="line">a.clear() <span class="comment">#删除列表中所有元素，但保留列表对象</span></span><br><span class="line"><span class="built_in">print</span>(a)</span><br><span class="line">``` python</span><br><span class="line">&gt;[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]</span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]</span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">0</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]</span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]</span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]</span><br><span class="line"><span class="number">0</span></span><br><span class="line"><span class="number">1</span></span><br><span class="line">[<span class="number">5</span>, <span class="number">4</span>, <span class="number">3</span>, <span class="number">2</span>, <span class="number">1</span>, <span class="number">6</span>, <span class="number">5</span>, <span class="number">4</span>, <span class="number">3</span>, <span class="number">2</span>, <span class="number">1</span>]</span><br><span class="line">[<span class="number">1</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">5</span>, <span class="number">6</span>]</span><br><span class="line">[<span class="number">1</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">5</span>, <span class="number">6</span>]</span><br><span class="line">[]</span><br><span class="line">&gt;* 列表的创建与删除</span><br><span class="line">&gt;<span class="number">1.</span>可以使用<span class="built_in">list</span>()函数将元组，<span class="built_in">range</span>()对象，字符串等可迭代类型的数据转换为列表</span><br><span class="line">&gt;<span class="number">2.</span>`<span class="built_in">range</span>([start,] stop [,step])`第一个参数默认值为<span class="number">0</span>，第二个参数表示终止值（不包括这个值），第三个表示步长（间隔）</span><br><span class="line">&gt;<span class="number">3.</span>当列表不再使用时，最好用<span class="keyword">del</span>命令删除列表，python删除该值</span><br><span class="line">```python</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a=<span class="built_in">list</span>((<span class="number">3</span>,<span class="number">5</span>,<span class="number">7</span>,<span class="number">9</span>))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">[<span class="number">3</span>, <span class="number">5</span>, <span class="number">7</span>, <span class="number">9</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">list</span>(<span class="built_in">range</span>(<span class="number">1</span>,<span class="number">10</span>,<span class="number">2</span>))</span><br><span class="line">[<span class="number">1</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">7</span>, <span class="number">9</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">list</span>(<span class="string">&quot;hello world&quot;</span>)</span><br><span class="line">[<span class="string">&#x27;h&#x27;</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;l&#x27;</span>, <span class="string">&#x27;l&#x27;</span>, <span class="string">&#x27;o&#x27;</span>, <span class="string">&#x27; &#x27;</span>, <span class="string">&#x27;w&#x27;</span>, <span class="string">&#x27;o&#x27;</span>, <span class="string">&#x27;r&#x27;</span>, <span class="string">&#x27;l&#x27;</span>, <span class="string">&#x27;d&#x27;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">del</span> a</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">Traceback (most recent call last):</span><br><span class="line">  File <span class="string">&quot;&lt;pyshell#5&gt;&quot;</span>, line <span class="number">1</span>, <span class="keyword">in</span> &lt;module&gt;</span><br><span class="line">    a</span><br><span class="line">NameError: name <span class="string">&#x27;a&#x27;</span> <span class="keyword">is</span> <span class="keyword">not</span> defined</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span></span><br></pre></td></tr></table></figure></li>
<li>列表元素的增加<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a=[<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a=a+[<span class="number">6</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">[<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.append(<span class="number">7</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">[<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">id</span>(a)       </span><br><span class="line"><span class="number">2173678801864</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a=[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]     <span class="comment">#python中是基于值的内存管理方式，变量修改值时，并不是直接修改变量的值，而是指向新的内存</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">id</span>(a)         <span class="comment">#重新定义首地址发生改变</span></span><br><span class="line"><span class="number">2173679010184</span> </span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b=[<span class="number">1</span>,<span class="number">2</span>,<span class="number">4</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a==b          </span><br><span class="line"><span class="literal">False</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">id</span>(a)==<span class="built_in">id</span>(b)</span><br><span class="line"><span class="literal">False</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">id</span>(a[<span class="number">0</span>])==<span class="built_in">id</span>(b[<span class="number">0</span>])    <span class="comment">#  两个列表中相同元素的首地址相同</span></span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">id</span>(a)</span><br><span class="line"><span class="number">2173679010184</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.append(<span class="number">5</span>)     <span class="comment">#append操作不改变首地址</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">id</span>(a)</span><br><span class="line"><span class="number">2173679010184</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.remove(<span class="number">5</span>)     <span class="comment">#remove操作不改变首地址</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">id</span>(a)</span><br><span class="line"><span class="number">2173679010184</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a[<span class="number">0</span>]=<span class="number">5</span>         <span class="comment">#直接赋值，也不改变首地址</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">[<span class="number">5</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">id</span>(a)</span><br><span class="line"><span class="number">2173679010184</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.extend([<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>])  <span class="comment">#添加若干列表元素，首地址仍然不变</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">[<span class="number">5</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">id</span>(a)</span><br><span class="line"><span class="number">2173679010184</span></span><br></pre></td></tr></table></figure></li>
<li>除非必要尽量避免使用insert在中间插入元素，最好使用append和pop函数<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a=[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">id</span>(a)</span><br><span class="line"><span class="number">1255750488008</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a=a*<span class="number">2</span>                <span class="comment">#列表乘法得到新列表其实是重新定义了一个列表，首地址发生改变</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">id</span>(a)</span><br><span class="line"><span class="number">1255750562056</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x=[[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]]*<span class="number">3</span>       <span class="comment">#当用*运算将列表重复创建时，并不创建元素的赋值，而是创建元素的引用，因此该变其中一个值时，相应的引用也会改变</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x</span><br><span class="line">[[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>], [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>], [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x[<span class="number">0</span>][<span class="number">0</span>]=<span class="number">10</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x</span><br><span class="line">[[<span class="number">10</span>, <span class="number">2</span>, <span class="number">3</span>], [<span class="number">10</span>, <span class="number">2</span>, <span class="number">3</span>], [<span class="number">10</span>, <span class="number">2</span>, <span class="number">3</span>]] </span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">id</span>(x[<span class="number">0</span>][<span class="number">0</span>])==<span class="built_in">id</span>(x[<span class="number">1</span>][<span class="number">0</span>])  <span class="comment">#这两个元素其实是一块内存</span></span><br><span class="line"><span class="literal">True</span>；</span><br><span class="line">```   </span><br><span class="line">&gt; * 列表元素的删除</span><br><span class="line">```python</span><br><span class="line">&gt;&gt;&gt;x = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">1</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> i <span class="keyword">in</span> x :</span><br><span class="line">	<span class="keyword">if</span> i ==<span class="number">1</span>:</span><br><span class="line">		x.remove(i)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x</span><br><span class="line">[<span class="number">2</span>, <span class="number">2</span>, <span class="number">2</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>]        <span class="comment">#列表有自动内存管理功能，当列表元素删除时，内存会自动收缩并移动使元素之间没有缝隙，元素的索引就改变了</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> i <span class="keyword">in</span> x:</span><br><span class="line">	<span class="keyword">if</span> (i ==<span class="number">1</span>):</span><br><span class="line">		x.remove(i)</span><br><span class="line">		x</span><br><span class="line">	</span><br><span class="line">[<span class="number">2</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span>]</span><br><span class="line">[<span class="number">2</span>, <span class="number">2</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span>]</span><br><span class="line">[<span class="number">2</span>, <span class="number">2</span>, <span class="number">1</span>, <span class="number">1</span>]</span><br><span class="line">[<span class="number">2</span>, <span class="number">2</span>, <span class="number">1</span>]</span><br><span class="line"><span class="comment">#为解决删除重复元素这个问题，可以采用从后向前的顺序来删除</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="built_in">len</span>(x)-<span class="number">1</span>,-<span class="number">1</span>,-<span class="number">1</span>):  <span class="comment">#len(x)-1 是最后一个元素下表，间隔-1</span></span><br><span class="line">	i</span><br><span class="line">	<span class="keyword">if</span>(x[i]==<span class="number">1</span>):</span><br><span class="line">		<span class="keyword">del</span> x[i]</span><br><span class="line">		x</span><br><span class="line">		</span><br><span class="line"><span class="number">6</span></span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">1</span>, <span class="number">1</span>]</span><br><span class="line"><span class="number">5</span></span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">1</span>]</span><br><span class="line"><span class="number">4</span></span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">1</span>, <span class="number">2</span>]</span><br><span class="line"><span class="number">3</span></span><br><span class="line"><span class="number">2</span></span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">2</span>]</span><br><span class="line"><span class="number">1</span></span><br><span class="line"><span class="number">0</span></span><br><span class="line">[<span class="number">2</span>, <span class="number">2</span>]</span><br></pre></td></tr></table></figure></li>
<li>切片操作<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">list</span> = [<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">9</span>,<span class="number">11</span>,<span class="number">13</span>,<span class="number">15</span>,<span class="number">17</span>]  <span class="comment">#第一个数字是起始下标</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">list</span>[ :  : ]                       <span class="comment">#第二个数字是终止下表</span></span><br><span class="line">[<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">9</span>, <span class="number">11</span>, <span class="number">13</span>, <span class="number">15</span>, <span class="number">17</span>]     <span class="comment">#第三个是间隔（默认为1）</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">list</span>[ : : -<span class="number">1</span>]</span><br><span class="line">[<span class="number">17</span>, <span class="number">15</span>, <span class="number">13</span>, <span class="number">11</span>, <span class="number">9</span>, <span class="number">7</span>, <span class="number">6</span>, <span class="number">5</span>, <span class="number">4</span>, <span class="number">3</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">list</span> [ : : <span class="number">2</span> ]</span><br><span class="line">[<span class="number">3</span>, <span class="number">5</span>, <span class="number">7</span>, <span class="number">11</span>, <span class="number">15</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">list</span> [<span class="number">3</span> : : ]</span><br><span class="line">[<span class="number">6</span>, <span class="number">7</span>, <span class="number">9</span>, <span class="number">11</span>, <span class="number">13</span>, <span class="number">15</span>, <span class="number">17</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">list</span>[<span class="number">3</span>: <span class="number">6</span>: <span class="number">1</span>]</span><br><span class="line">[<span class="number">6</span>, <span class="number">7</span>, <span class="number">9</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]      <span class="comment">#通过切片增加元素</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a[<span class="built_in">len</span>(a):]</span><br><span class="line">[]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a[<span class="built_in">len</span>(a):]=[<span class="number">4</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a[ : <span class="number">3</span>]=[<span class="number">0</span>]     </span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">[<span class="number">0</span>, <span class="number">4</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">del</span> a[ : <span class="number">1</span>]         <span class="comment">#切片删除元素</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">[<span class="number">4</span>]</span><br></pre></td></tr></table></figure></li>
<li>列表排序<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>,<span class="number">10</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> random</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>random.shuffle(a)       <span class="comment">#打乱顺序</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">[<span class="number">4</span>, <span class="number">6</span>, <span class="number">8</span>, <span class="number">7</span>, <span class="number">10</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">9</span>, <span class="number">2</span>, <span class="number">5</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.sort()    <span class="comment">#  升序排列</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">10</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a. sort (reverse = <span class="literal">True</span>)   <span class="comment">#降序排列</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">[<span class="number">10</span>, <span class="number">9</span>, <span class="number">8</span>, <span class="number">7</span>, <span class="number">6</span>, <span class="number">5</span>, <span class="number">4</span>, <span class="number">3</span>, <span class="number">2</span>, <span class="number">1</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">sorted</span>(a)        <span class="comment">#升序排列，返回新列表，原列表不变</span></span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">10</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">[<span class="number">10</span>, <span class="number">9</span>, <span class="number">8</span>, <span class="number">7</span>, <span class="number">6</span>, <span class="number">5</span>, <span class="number">4</span>, <span class="number">3</span>, <span class="number">2</span>, <span class="number">1</span>]</span><br></pre></td></tr></table></figure></li>
<li>简单的列表推导式<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = [x*x <span class="keyword">for</span> x <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">10</span>)]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">[<span class="number">0</span>, <span class="number">1</span>, <span class="number">4</span>, <span class="number">9</span>, <span class="number">16</span>, <span class="number">25</span>, <span class="number">36</span>, <span class="number">49</span>, <span class="number">64</span>, <span class="number">81</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>[(x , y) <span class="keyword">for</span>  x <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">2</span>) <span class="keyword">for</span> y <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">3</span>)]</span><br><span class="line">[(<span class="number">0</span>, <span class="number">0</span>), (<span class="number">0</span>, <span class="number">1</span>), (<span class="number">0</span>, <span class="number">2</span>), (<span class="number">1</span>, <span class="number">0</span>), (<span class="number">1</span>, <span class="number">1</span>), (<span class="number">1</span>, <span class="number">2</span>)]</span><br></pre></td></tr></table></figure>
<h2 id="2-元组"><a href="#2-元组" class="headerlink" title="2.元组"></a>2.元组</h2></li>
<li>元组属于不可变序列，一旦创建，用任何方法都不能修改元素的值，也无法增加或删除元素</li>
<li>元组的创建与删除<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = (<span class="string">&#x27;a&#x27;</span>,)   <span class="comment">#如果要创建只包含一个元素时，后面要加一个‘，’逗号</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">(<span class="string">&#x27;a&#x27;</span>,)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = (<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;z&#x27;</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">(<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;z&#x27;</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = <span class="number">1</span>,<span class="number">2</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">(<span class="number">1</span>, <span class="number">2</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">tuple</span>(<span class="string">&#x27;abcdef&#x27;</span>)      <span class="comment">#可以把其他类型的转化为元组</span></span><br><span class="line">(<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;d&#x27;</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;f&#x27;</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">del</span> a                <span class="comment">#删除整个元组</span></span><br></pre></td></tr></table></figure></li>
<li>元组和列表的区别：<br>tuple（）是冻结列表使其不可变，list（）是融化元组使其可变，使用元组可以是代码数据更安全，如果元组中包含列表，情况略有不同<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>x = ( [<span class="number">1</span>,<span class="number">2</span>] , <span class="number">3</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x[<span class="number">0</span>][<span class="number">0</span>]=<span class="number">5</span>                  <span class="comment">#可以对其中的列表进行列表的操作</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x</span><br><span class="line">([<span class="number">5</span>, <span class="number">2</span>], <span class="number">3</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x[<span class="number">0</span>].append(<span class="number">8</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x</span><br><span class="line">([<span class="number">5</span>, <span class="number">2</span>, <span class="number">8</span>], <span class="number">3</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x[<span class="number">0</span>] = x[<span class="number">0</span>] + [<span class="number">10</span>]     <span class="comment">#但不能这样，元组不可改变</span></span><br><span class="line">Traceback (most recent call last):</span><br><span class="line">  File <span class="string">&quot;&lt;pyshell#18&gt;&quot;</span>, line <span class="number">1</span>, <span class="keyword">in</span> &lt;module&gt;</span><br><span class="line">    x[<span class="number">0</span>] = x[<span class="number">0</span>] + [<span class="number">10</span>]</span><br><span class="line">TypeError: <span class="string">&#x27;tuple&#x27;</span> <span class="built_in">object</span> does <span class="keyword">not</span> support item assignment</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span></span><br></pre></td></tr></table></figure></li>
<li>序列解包<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>x ,y ,z = <span class="number">1</span> ,<span class="number">2</span> , <span class="number">3</span>    <span class="comment">#对多个变量同时赋值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x,y,z</span><br><span class="line">(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">print</span>(x,y,z)</span><br><span class="line"><span class="number">1</span> <span class="number">2</span> <span class="number">3</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>keys = [<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>values = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> k , v <span class="keyword">in</span> <span class="built_in">zip</span>(keys,values):    <span class="comment">#zip（）是将多个列表或元组对应位置的元素结合成元组，并返回包含这些元组的列表</span></span><br><span class="line">	<span class="built_in">print</span>(k,v)                    <span class="comment">#使用序列解包可以很方便的同时遍历多个序列</span></span><br><span class="line">a <span class="number">1</span></span><br><span class="line">b <span class="number">2</span></span><br><span class="line">c <span class="number">3</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">list</span>(<span class="built_in">zip</span>(keys,values))</span><br><span class="line">[(<span class="string">&#x27;a&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;b&#x27;</span>, <span class="number">2</span>), (<span class="string">&#x27;c&#x27;</span>, <span class="number">3</span>)]</span><br></pre></td></tr></table></figure></li>
<li>生成器推导式<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>g = ((i+<span class="number">2</span>)**<span class="number">2</span> <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">10</span>))   <span class="comment">#生成器推导式的结果是一个对象需要用tuple或list来承接</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>g</span><br><span class="line">&lt;generator <span class="built_in">object</span> &lt;genexpr&gt; at <span class="number">0x0000015E92B34F10</span>&gt;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">tuple</span>(g)</span><br><span class="line">(<span class="number">4</span>, <span class="number">9</span>, <span class="number">16</span>, <span class="number">25</span>, <span class="number">36</span>, <span class="number">49</span>, <span class="number">64</span>, <span class="number">81</span>, <span class="number">100</span>, <span class="number">121</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">tuple</span>()                   <span class="comment">#所有元素访问元素结束后，如果需要重新访问其中的元素，必须重新创建该生成器对象</span></span><br><span class="line">()</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>g = ((i+<span class="number">2</span>)**<span class="number">2</span> <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">10</span>))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">list</span>(g)</span><br><span class="line">[<span class="number">4</span>, <span class="number">9</span>, <span class="number">16</span>, <span class="number">25</span>, <span class="number">36</span>, <span class="number">49</span>, <span class="number">64</span>, <span class="number">81</span>, <span class="number">100</span>, <span class="number">121</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>g = ((i+<span class="number">2</span>)**<span class="number">2</span> <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">10</span>))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>g.__next__()   <span class="comment">#访问下一个元素</span></span><br><span class="line"><span class="number">4</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>g.__next__()</span><br><span class="line"><span class="number">9</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>g = ((i+<span class="number">2</span>)**<span class="number">2</span> <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">10</span>))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> i <span class="keyword">in</span> g:                  <span class="comment">#循环打印</span></span><br><span class="line">	<span class="built_in">print</span>(<span class="built_in">str</span>(i) + <span class="string">&#x27;,&#x27;</span>,end=<span class="string">&#x27;&#x27;</span>)</span><br><span class="line"><span class="number">4</span>,<span class="number">9</span>,<span class="number">16</span>,<span class="number">25</span>,<span class="number">36</span>,<span class="number">49</span>,<span class="number">64</span>,<span class="number">81</span>,<span class="number">100</span>,<span class="number">121</span>,</span><br></pre></td></tr></table></figure>
<h2 id="3-字典"><a href="#3-字典" class="headerlink" title="3.字典"></a>3.字典</h2></li>
<li>字典是“键值对”的无序可变序列，键和值用冒号分隔，所有元素放在大括号中，键是不可变数据，不能用列表、集合、字典作为字典的键，这些是可变序列，键不允许重复，值可以重复。<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a =&#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b = <span class="string">&#x27;hello world.&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="function"><span class="keyword">def</span> <span class="title">demo</span>():</span></span><br><span class="line">	a = <span class="number">3</span></span><br><span class="line">	b = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line">	<span class="built_in">print</span>(<span class="string">&#x27;locals&#x27;</span>,<span class="built_in">locals</span>())  <span class="comment">#内置函数local（）返回包含当前作用域内所有的局部变量和值的字典</span></span><br><span class="line">	<span class="built_in">print</span>(<span class="string">&#x27;globals&#x27;</span>,<span class="built_in">globals</span>())  <span class="comment">#内置函数globals（）返回和查看当前作用域内所有的全局变量和值的字典</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>demo()</span><br><span class="line"><span class="built_in">locals</span> &#123;<span class="string">&#x27;b&#x27;</span>: [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>], <span class="string">&#x27;a&#x27;</span>: <span class="number">3</span>&#125;</span><br><span class="line"><span class="built_in">globals</span> &#123;<span class="string">&#x27;__name__&#x27;</span>: <span class="string">&#x27;__main__&#x27;</span>, <span class="string">&#x27;__doc__&#x27;</span>: <span class="literal">None</span>, <span class="string">&#x27;__package__&#x27;</span>: <span class="literal">None</span>, <span class="string">&#x27;__loader__&#x27;</span>: &lt;<span class="class"><span class="keyword">class</span> &#x27;<span class="title">_frozen_importlib</span>.<span class="title">BuiltinImporter</span>&#x27;&gt;, &#x27;<span class="title">__spec__</span>&#x27;:</span> <span class="literal">None</span>, <span class="string">&#x27;__annotations__&#x27;</span>: &#123;&#125;, <span class="string">&#x27;__builtins__&#x27;</span>: &lt;module <span class="string">&#x27;builtins&#x27;</span> (built-<span class="keyword">in</span>)&gt;, <span class="string">&#x27;a&#x27;</span>: &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;, <span class="string">&#x27;b&#x27;</span>: <span class="string">&#x27;hello world.&#x27;</span>, <span class="string">&#x27;demo&#x27;</span>: &lt;function demo at <span class="number">0x000001B02EDE51E0</span>&gt;&#125;</span><br></pre></td></tr></table></figure></li>
<li>字典的创建与删除<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a =&#123;<span class="string">&#x27;a&#x27;</span> : <span class="number">1</span>, <span class="string">&#x27;b&#x27;</span> :  <span class="number">2</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">&#123;<span class="string">&#x27;a&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;b&#x27;</span>: <span class="number">2</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>k = &#123;<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>v = &#123;<span class="number">1</span> ,<span class="number">2</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dictionary = <span class="built_in">dict</span>(<span class="built_in">zip</span>(k,v))    <span class="comment">#dict()根据已有的数据快速创建字典</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dictionary</span><br><span class="line">&#123;<span class="string">&#x27;a&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;b&#x27;</span>: <span class="number">2</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">zip</span>(k,v)</span><br><span class="line">&lt;<span class="built_in">zip</span> <span class="built_in">object</span> at <span class="number">0x000001B02EDCA288</span>&gt;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">list</span>(<span class="built_in">zip</span>(k,v))</span><br><span class="line">[(<span class="string">&#x27;a&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;b&#x27;</span>, <span class="number">2</span>)]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">tuple</span>(<span class="built_in">zip</span>(k,v))</span><br><span class="line">((<span class="string">&#x27;a&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;b&#x27;</span>, <span class="number">2</span>))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d = <span class="built_in">dict</span>(name = <span class="string">&#x27;wd&#x27;</span> ,age = <span class="number">18</span>)  <span class="comment">#dict()根据给定的“键值对”来创建字典</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d</span><br><span class="line">&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;wd&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>adict = <span class="built_in">dict</span>.fromkeys([<span class="string">&#x27;name&#x27;</span>,<span class="string">&#x27;age&#x27;</span>,<span class="string">&#x27;sex&#x27;</span>])  <span class="comment">#给定内容为“键”，创建值为空的字典</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>adict</span><br><span class="line">&#123;<span class="string">&#x27;name&#x27;</span>: <span class="literal">None</span>, <span class="string">&#x27;age&#x27;</span>: <span class="literal">None</span>, <span class="string">&#x27;sex&#x27;</span>: <span class="literal">None</span>&#125;</span><br></pre></td></tr></table></figure></li>
<li>字典元素的读取<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>dic = &#123;<span class="string">&#x27;name&#x27;</span> : <span class="string">&#x27;wd&#x27;</span>, <span class="string">&#x27;sex&#x27;</span> : <span class="string">&#x27;male&#x27;</span> , <span class="string">&#x27;age&#x27;</span> : <span class="number">18</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dic</span><br><span class="line">&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;wd&#x27;</span>, <span class="string">&#x27;sex&#x27;</span>: <span class="string">&#x27;male&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dic[<span class="string">&#x27;name&#x27;</span>]      <span class="comment">#下表为键</span></span><br><span class="line"><span class="string">&#x27;wd&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">print</span>(dic.get(<span class="string">&#x27;address&#x27;</span>))    <span class="comment">#使用get（）方法访问键对应的值</span></span><br><span class="line"><span class="literal">None</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dic[<span class="string">&#x27;score&#x27;</span>]=dic.get(<span class="string">&#x27;score&#x27;</span>,[])    <span class="comment">#get（）也可这样添加键值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dic</span><br><span class="line">&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;wd&#x27;</span>, <span class="string">&#x27;sex&#x27;</span>: <span class="string">&#x27;male&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>, <span class="string">&#x27;score&#x27;</span>: []&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dic[<span class="string">&#x27;score&#x27;</span>].append(<span class="number">100</span>)     <span class="comment">#使用列表方法添加元素</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dic[<span class="string">&#x27;score&#x27;</span>].append(<span class="number">98</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dic</span><br><span class="line">&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;wd&#x27;</span>, <span class="string">&#x27;sex&#x27;</span>: <span class="string">&#x27;male&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>, <span class="string">&#x27;score&#x27;</span>: [<span class="number">100</span>, <span class="number">98</span>]&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> item <span class="keyword">in</span> dic.items():     <span class="comment">#items（）方法返回字典的“键值”对列表</span></span><br><span class="line">	<span class="built_in">print</span>(item)</span><br><span class="line"></span><br><span class="line">	</span><br><span class="line">(<span class="string">&#x27;name&#x27;</span>, <span class="string">&#x27;wd&#x27;</span>)</span><br><span class="line">(<span class="string">&#x27;sex&#x27;</span>, <span class="string">&#x27;male&#x27;</span>)</span><br><span class="line">(<span class="string">&#x27;age&#x27;</span>, <span class="number">18</span>)</span><br><span class="line">(<span class="string">&#x27;score&#x27;</span>, [<span class="number">100</span>, <span class="number">98</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> key <span class="keyword">in</span> dic.keys():   <span class="comment">#keys（）方法返回字典的键的列表</span></span><br><span class="line">	<span class="built_in">print</span>(key)</span><br><span class="line">	</span><br><span class="line">name</span><br><span class="line">sex</span><br><span class="line">age</span><br><span class="line">score</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> key,value <span class="keyword">in</span> dic.items():</span><br><span class="line">	<span class="built_in">print</span>(key,value)</span><br><span class="line">	</span><br><span class="line">name wd</span><br><span class="line">sex male</span><br><span class="line">age <span class="number">18</span></span><br><span class="line">score [<span class="number">100</span>, <span class="number">98</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">print</span>(dic.keys())</span><br><span class="line">dict_keys([<span class="string">&#x27;name&#x27;</span>, <span class="string">&#x27;sex&#x27;</span>, <span class="string">&#x27;age&#x27;</span>, <span class="string">&#x27;score&#x27;</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">print</span>(dic.values())      <span class="comment">#values（）方法可以返回字典的值的列表</span></span><br><span class="line">dict_values([<span class="string">&#x27;wd&#x27;</span>, <span class="string">&#x27;male&#x27;</span>, <span class="number">18</span>, [<span class="number">100</span>, <span class="number">98</span>]])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span></span><br></pre></td></tr></table></figure></li>
<li>字典元素的添加与修改<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>dic = &#123;<span class="string">&#x27;name&#x27;</span> : <span class="string">&#x27;wd&#x27;</span>, <span class="string">&#x27;age&#x27;</span> : <span class="number">18</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dic</span><br><span class="line">&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;wd&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dic[<span class="string">&#x27;sex&#x27;</span>] = <span class="string">&#x27;male&#x27;</span>    <span class="comment">#当以指定的‘键’为下标给字典赋值时，若键值存在，则表示修改值，若不存在，则表示添加一个键值对，添加一个新元素</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dic</span><br><span class="line">&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;wd&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>, <span class="string">&#x27;sex&#x27;</span>: <span class="string">&#x27;male&#x27;</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dic.items()</span><br><span class="line">dict_items([(<span class="string">&#x27;name&#x27;</span>, <span class="string">&#x27;wd&#x27;</span>), (<span class="string">&#x27;age&#x27;</span>, <span class="number">18</span>), (<span class="string">&#x27;sex&#x27;</span>, <span class="string">&#x27;male&#x27;</span>)])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dic.update(&#123;<span class="string">&#x27;a&#x27;</span> :<span class="number">1</span>,<span class="string">&#x27;b&#x27;</span> : <span class="number">2</span>&#125;) <span class="comment">#使用update()方法是一次性将另一个字典的键值对全部添加到当前字典对象</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dic</span><br><span class="line">&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;wd&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>, <span class="string">&#x27;sex&#x27;</span>: <span class="string">&#x27;male&#x27;</span>, <span class="string">&#x27;a&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;b&#x27;</span>: <span class="number">2</span>&#125;</span><br></pre></td></tr></table></figure></li>
<li>字典应用案例—-统计数据<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> string</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> random</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x = string.ascii_letters+string.digits+string.punctuation  <span class="comment">#添加元素字符串</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x</span><br><span class="line"><span class="string">&#x27;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!&quot;#$%&amp;\&#x27;()*+,-./:;&lt;=&gt;?@[\\]^_`&#123;|&#125;~&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y = [random.choice(x) <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">100</span>)]  <span class="comment">#随机100个x中的元素</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y</span><br><span class="line">[<span class="string">&#x27;D&#x27;</span>, <span class="string">&#x27;#&#x27;</span>, <span class="string">&#x27;B&#x27;</span>, <span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;~&#x27;</span>, <span class="string">&#x27;9&#x27;</span>, <span class="string">&#x27;B&#x27;</span>, <span class="string">&#x27;F&#x27;</span>, <span class="string">&#x27;i&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;/&#x27;</span>, <span class="string">&#x27;~&#x27;</span>, <span class="string">&#x27;%&#x27;</span>, <span class="string">&#x27;C&#x27;</span>, <span class="string">&#x27;x&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;7&#x27;</span>, <span class="string">&#x27;H&#x27;</span>, <span class="string">&#x27;R&#x27;</span>, <span class="string">&#x27;i&#x27;</span>, <span class="string">&#x27;k&#x27;</span>, <span class="string">&#x27;-&#x27;</span>, <span class="string">&#x27;K&#x27;</span>, <span class="string">&#x27;/&#x27;</span>, <span class="string">&#x27;d&#x27;</span>, <span class="string">&#x27;r&#x27;</span>, <span class="string">&#x27;T&#x27;</span>, <span class="string">&#x27;O&#x27;</span>, <span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;&amp;&#x27;</span>, <span class="string">&#x27;/&#x27;</span>, <span class="string">&#x27;l&#x27;</span>, <span class="string">&#x27;s&#x27;</span>, <span class="string">&#x27;t&#x27;</span>, <span class="string">&quot;&#x27;&quot;</span>, <span class="string">&#x27;?&#x27;</span>, <span class="string">&#x27;o&#x27;</span>, <span class="string">&#x27;(&#x27;</span>, <span class="string">&#x27;j&#x27;</span>, <span class="string">&quot;&#x27;&quot;</span>, <span class="string">&#x27;R&#x27;</span>, <span class="string">&#x27;F&#x27;</span>, <span class="string">&#x27;_&#x27;</span>, <span class="string">&#x27;W&#x27;</span>, <span class="string">&#x27;F&#x27;</span>, <span class="string">&#x27;W&#x27;</span>, <span class="string">&#x27;O&#x27;</span>, <span class="string">&#x27;w&#x27;</span>, <span class="string">&#x27;]&#x27;</span>, <span class="string">&#x27;j&#x27;</span>, <span class="string">&#x27;t&#x27;</span>, <span class="string">&#x27;P&#x27;</span>, <span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;k&#x27;</span>, <span class="string">&#x27;w&#x27;</span>, <span class="string">&#x27;,&#x27;</span>, <span class="string">&#x27;|&#x27;</span>, <span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;|&#x27;</span>, <span class="string">&#x27;+&#x27;</span>, <span class="string">&#x27;X&#x27;</span>, <span class="string">&#x27;R&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;&amp;&#x27;</span>, <span class="string">&#x27;W&#x27;</span>, <span class="string">&#x27;U&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;+&#x27;</span>, <span class="string">&#x27;W&#x27;</span>, <span class="string">&#x27;6&#x27;</span>, <span class="string">&#x27;&quot;&#x27;</span>, <span class="string">&#x27;]&#x27;</span>, <span class="string">&#x27;X&#x27;</span>, <span class="string">&#x27;`&#x27;</span>, <span class="string">&#x27;o&#x27;</span>, <span class="string">&#x27;x&#x27;</span>, <span class="string">&#x27;&lt;&#x27;</span>, <span class="string">&#x27;$&#x27;</span>, <span class="string">&#x27;S&#x27;</span>, <span class="string">&#x27;_&#x27;</span>, <span class="string">&#x27;&quot;&#x27;</span>, <span class="string">&#x27;-&#x27;</span>, <span class="string">&#x27;Q&#x27;</span>, <span class="string">&#x27;R&#x27;</span>, <span class="string">&#x27;I&#x27;</span>, <span class="string">&#x27;q&#x27;</span>, <span class="string">&#x27;x&#x27;</span>, <span class="string">&#x27;[&#x27;</span>, <span class="string">&#x27;8&#x27;</span>, <span class="string">&#x27;G&#x27;</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;Q&#x27;</span>, <span class="string">&#x27;i&#x27;</span>, <span class="string">&#x27;`&#x27;</span>, <span class="string">&#x27;7&#x27;</span>, <span class="string">&#x27;d&#x27;</span>, <span class="string">&#x27;u&#x27;</span>, <span class="string">&#x27;A&#x27;</span>, <span class="string">&#x27;&lt;&#x27;</span>, <span class="string">&#x27;%&#x27;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>z =<span class="string">&#x27; &#x27;</span>.join(y)  <span class="comment">#将列表转化成字符串</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>z</span><br><span class="line"><span class="string">&#x27;D # B 4 ~ 9 B F i b / ~ % C x 3 7 H R i k - K / d r T O 4 &amp; / l s t \&#x27; ? o ( j \&#x27; R F _ W F W O w ] j t P 1 k w , | 4 | + X R 3 &amp; W U 3 + W 6 &quot; ] X ` o x &lt; $ S _ &quot; - Q R I q x [ 8 G e Q i ` 7 d u A &lt; %&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d = <span class="built_in">dict</span>()  <span class="comment">#创建字典</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> ch <span class="keyword">in</span> z:         <span class="comment">#循环遍历</span></span><br><span class="line">	d[ch] = d.get(ch,<span class="number">0</span>)+<span class="number">1</span>    <span class="comment">#如果有键，则在原来基础上+1，如果没有则创建键，并使值初始为0 + 1</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d</span><br><span class="line">&#123;<span class="string">&#x27;D&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27; &#x27;</span>: <span class="number">99</span>, <span class="string">&#x27;#&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;B&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;4&#x27;</span>: <span class="number">3</span>, <span class="string">&#x27;~&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;9&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;F&#x27;</span>: <span class="number">3</span>, <span class="string">&#x27;i&#x27;</span>: <span class="number">3</span>, <span class="string">&#x27;b&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;/&#x27;</span>: <span class="number">3</span>, <span class="string">&#x27;%&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;C&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;x&#x27;</span>: <span class="number">3</span>, <span class="string">&#x27;3&#x27;</span>: <span class="number">3</span>, <span class="string">&#x27;7&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;H&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;R&#x27;</span>: <span class="number">4</span>, <span class="string">&#x27;k&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;-&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;K&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;d&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;r&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;T&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;O&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;&amp;&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;l&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;s&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;t&#x27;</span>: <span class="number">2</span>, <span class="string">&quot;&#x27;&quot;</span>: <span class="number">2</span>, <span class="string">&#x27;?&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;o&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;(&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;j&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;_&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;W&#x27;</span>: <span class="number">4</span>, <span class="string">&#x27;w&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;]&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;P&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;1&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;,&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;|&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;+&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;X&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;U&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;6&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;&quot;&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;`&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;&lt;&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;$&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;S&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;Q&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;I&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;q&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;[&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;8&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;G&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;e&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;u&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;A&#x27;</span>: <span class="number">1</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">from</span> collections <span class="keyword">import</span> defaultdict  <span class="comment">#也可使用collections的defaulydict类来实现该功能</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>frequences = defaultdict(<span class="built_in">int</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>frequences</span><br><span class="line">defaultdict(&lt;<span class="class"><span class="keyword">class</span> &#x27;<span class="title">int</span>&#x27;&gt;, &#123;&#125;)</span></span><br><span class="line"><span class="class">&gt;&gt;&gt; <span class="title">for</span> <span class="title">item</span> <span class="title">in</span> <span class="title">z</span>:</span></span><br><span class="line">	frequences[item]+=<span class="number">1</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>frequences.items()</span><br><span class="line">dict_items([(<span class="string">&#x27;D&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27; &#x27;</span>, <span class="number">99</span>), (<span class="string">&#x27;#&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;B&#x27;</span>, <span class="number">2</span>), (<span class="string">&#x27;4&#x27;</span>, <span class="number">3</span>), (<span class="string">&#x27;~&#x27;</span>, <span class="number">2</span>), (<span class="string">&#x27;9&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;F&#x27;</span>, <span class="number">3</span>), (<span class="string">&#x27;i&#x27;</span>, <span class="number">3</span>), (<span class="string">&#x27;b&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;/&#x27;</span>, <span class="number">3</span>), (<span class="string">&#x27;%&#x27;</span>, <span class="number">2</span>), (<span class="string">&#x27;C&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;x&#x27;</span>, <span class="number">3</span>), (<span class="string">&#x27;3&#x27;</span>, <span class="number">3</span>), (<span class="string">&#x27;7&#x27;</span>, <span class="number">2</span>), (<span class="string">&#x27;H&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;R&#x27;</span>, <span class="number">4</span>), (<span class="string">&#x27;k&#x27;</span>, <span class="number">2</span>), (<span class="string">&#x27;-&#x27;</span>, <span class="number">2</span>), (<span class="string">&#x27;K&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;d&#x27;</span>, <span class="number">2</span>), (<span class="string">&#x27;r&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;T&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;O&#x27;</span>, <span class="number">2</span>), (<span class="string">&#x27;&amp;&#x27;</span>, <span class="number">2</span>), (<span class="string">&#x27;l&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;s&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;t&#x27;</span>, <span class="number">2</span>), (<span class="string">&quot;&#x27;&quot;</span>, <span class="number">2</span>), (<span class="string">&#x27;?&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;o&#x27;</span>, <span class="number">2</span>), (<span class="string">&#x27;(&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;j&#x27;</span>, <span class="number">2</span>), (<span class="string">&#x27;_&#x27;</span>, <span class="number">2</span>), (<span class="string">&#x27;W&#x27;</span>, <span class="number">4</span>), (<span class="string">&#x27;w&#x27;</span>, <span class="number">2</span>), (<span class="string">&#x27;]&#x27;</span>, <span class="number">2</span>), (<span class="string">&#x27;P&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;1&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;,&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;|&#x27;</span>, <span class="number">2</span>), (<span class="string">&#x27;+&#x27;</span>, <span class="number">2</span>), (<span class="string">&#x27;X&#x27;</span>, <span class="number">2</span>), (<span class="string">&#x27;U&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;6&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;&quot;&#x27;</span>, <span class="number">2</span>), (<span class="string">&#x27;`&#x27;</span>, <span class="number">2</span>), (<span class="string">&#x27;&lt;&#x27;</span>, <span class="number">2</span>), (<span class="string">&#x27;$&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;S&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;Q&#x27;</span>, <span class="number">2</span>), (<span class="string">&#x27;I&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;q&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;[&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;8&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;G&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;e&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;u&#x27;</span>, <span class="number">1</span>), (<span class="string">&#x27;A&#x27;</span>, <span class="number">1</span>)])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">from</span> collections <span class="keyword">import</span> Counter  <span class="comment">#也可使用Collection类快速寻找出现次数最多的元素</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>frequences = Counter(z)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>frequences.items</span><br><span class="line">&lt;built-<span class="keyword">in</span> method items of Counter <span class="built_in">object</span> at <span class="number">0x0000026723A64F10</span>&gt;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>frequences.most_common(<span class="number">1</span>)</span><br><span class="line">[(<span class="string">&#x27; &#x27;</span>, <span class="number">99</span>)]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>frequences.most_common(<span class="number">3</span>)</span><br><span class="line">[(<span class="string">&#x27; &#x27;</span>, <span class="number">99</span>), (<span class="string">&#x27;R&#x27;</span>, <span class="number">4</span>), (<span class="string">&#x27;W&#x27;</span>, <span class="number">4</span>)]</span><br></pre></td></tr></table></figure>
<h2 id="4-集合"><a href="#4-集合" class="headerlink" title="4.集合"></a>4.集合</h2></li>
<li>集合是无序可变序列，与字典一样使用一对大括号，同一个集合的元素之间不允许重复，集合中每一个元素都是唯一的</li>
<li>集合的创建与删除<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a =&#123;<span class="number">3</span>,<span class="number">5</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.add(<span class="number">6</span>)   <span class="comment">#添加元素</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">&#123;<span class="number">3</span>, <span class="number">5</span>, <span class="number">6</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">set</span>([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>])     <span class="comment">#将其他类型的数据转化为集合</span></span><br><span class="line">&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">set</span>([<span class="number">0</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">0</span>,<span class="number">2</span>,<span class="number">1</span>,<span class="number">3</span>])      <span class="comment">#若有重复元素，只保留一个</span></span><br><span class="line">&#123;<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.pop()     <span class="comment">#删除一个元素  （此方法不能指定元素，不能带参数）</span></span><br><span class="line"><span class="number">3</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">&#123;<span class="number">5</span>, <span class="number">6</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.remove(<span class="number">5</span>)   <span class="comment">#删除指定元素</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">&#123;<span class="number">6</span>&#125;</span><br></pre></td></tr></table></figure></li>
<li>集合操作<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a =&#123;<span class="number">8</span>,<span class="number">9</span>,<span class="number">10</span>,<span class="number">11</span>,<span class="number">12</span>,<span class="number">113</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b=&#123;<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>,<span class="number">10</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a | b     <span class="comment">#并集 </span></span><br><span class="line">&#123;<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>, <span class="number">113</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.union(b)</span><br><span class="line">&#123;<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>, <span class="number">113</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a &amp; b       <span class="comment">#交集</span></span><br><span class="line">&#123;<span class="number">8</span>, <span class="number">9</span>, <span class="number">10</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.intersection(b)</span><br><span class="line">&#123;<span class="number">8</span>, <span class="number">9</span>, <span class="number">10</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.difference(b)       <span class="comment">#差集</span></span><br><span class="line">&#123;<span class="number">113</span>, <span class="number">11</span>, <span class="number">12</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a - b</span><br><span class="line">&#123;<span class="number">113</span>, <span class="number">11</span>, <span class="number">12</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.symmetric_difference(b)     <span class="comment">#对称差</span></span><br><span class="line">&#123;<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">11</span>, <span class="number">12</span>, <span class="number">113</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a ^ b</span><br><span class="line">&#123;<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">11</span>, <span class="number">12</span>, <span class="number">113</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a == b     <span class="comment">#判断比较两个集合的大小</span></span><br><span class="line"><span class="literal">False</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.issubset(b)     <span class="comment">#判断是否为子集</span></span><br><span class="line"><span class="literal">False</span></span><br></pre></td></tr></table></figure>
<h2 id="5-内置方法sorted"><a href="#5-内置方法sorted" class="headerlink" title="5.内置方法sorted()"></a>5.内置方法sorted()</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>persons = [&#123;<span class="string">&#x27;name&#x27;</span> : <span class="string">&#x27;Dong&#x27;</span> , <span class="string">&#x27;age&#x27;</span> :  <span class="number">37</span> &#125;,&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;Zhang&#x27;</span>,<span class="string">&#x27;age&#x27;</span>: <span class="number">40</span>&#125;,&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;Li&#x27;</span>,<span class="string">&#x27;age&#x27;</span>: <span class="number">50</span>&#125;,&#123;<span class="string">&#x27;name&#x27;</span>:<span class="string">&#x27;Dong&#x27;</span>,<span class="string">&#x27;age&#x27;</span> : <span class="number">43</span>&#125;]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">print</span>(persons)</span><br><span class="line">[&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;Dong&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">37</span>&#125;, &#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;Zhang&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">40</span>&#125;, &#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;Li&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">50</span>&#125;, &#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;Dong&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">43</span>&#125;]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">print</span>(<span class="built_in">sorted</span>(persons,key=<span class="keyword">lambda</span> x:(x[<span class="string">&#x27;name&#x27;</span>],-x[<span class="string">&#x27;age&#x27;</span>]))) <span class="comment">#使用key，先按姓名的升序排序，再按年龄的降序排列</span></span><br><span class="line">[&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;Dong&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">43</span>&#125;, &#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;Dong&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">37</span>&#125;, &#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;Li&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">50</span>&#125;, &#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;Zhang&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">40</span>&#125;]</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>phonebook = &#123;<span class="string">&#x27;Linda&#x27;</span>:<span class="string">&#x27;7750&#x27;</span>,<span class="string">&#x27;Bob&#x27;</span>:<span class="string">&#x27;9345&#x27;</span>,<span class="string">&#x27;Carol&#x27;</span>:<span class="string">&#x27;5834&#x27;</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">from</span> operator <span class="keyword">import</span> itemgetter</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">sorted</span>(phonebook.items(),key = itemgetter(<span class="number">1</span>)) <span class="comment">#按字典中元素值进行排序</span></span><br><span class="line">[(<span class="string">&#x27;Carol&#x27;</span>, <span class="string">&#x27;5834&#x27;</span>), (<span class="string">&#x27;Linda&#x27;</span>, <span class="string">&#x27;7750&#x27;</span>), (<span class="string">&#x27;Bob&#x27;</span>, <span class="string">&#x27;9345&#x27;</span>)]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">sorted</span>(phonebook.items(),key = itemgetter(<span class="number">0</span>)) <span class="comment">#按字典中元素键进行排序</span></span><br><span class="line">[(<span class="string">&#x27;Bob&#x27;</span>, <span class="string">&#x27;9345&#x27;</span>), (<span class="string">&#x27;Carol&#x27;</span>, <span class="string">&#x27;5834&#x27;</span>), (<span class="string">&#x27;Linda&#x27;</span>, <span class="string">&#x27;7750&#x27;</span>)]</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>gameresult = [[<span class="string">&#x27;Bob&#x27;</span>,<span class="number">95.0</span>,<span class="string">&#x27;A&#x27;</span>],[<span class="string">&#x27;Alan&#x27;</span>,<span class="number">86.0</span>,<span class="string">&#x27;C&#x27;</span>],[<span class="string">&#x27;Mandy&#x27;</span>,<span class="number">83.5</span>,<span class="string">&#x27;A&#x27;</span>],[<span class="string">&#x27;Rob&#x27;</span>,<span class="number">89.3</span>,<span class="string">&#x27;E&#x27;</span>]]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">sorted</span>(gameresult,key = itemgetter(<span class="number">1</span>,<span class="number">0</span>)) <span class="comment">#按分数升序，相同按姓名升序</span></span><br><span class="line">[[<span class="string">&#x27;Mandy&#x27;</span>, <span class="number">83.5</span>, <span class="string">&#x27;A&#x27;</span>], [<span class="string">&#x27;Alan&#x27;</span>, <span class="number">86.0</span>, <span class="string">&#x27;C&#x27;</span>], [<span class="string">&#x27;Rob&#x27;</span>, <span class="number">89.3</span>, <span class="string">&#x27;E&#x27;</span>], [<span class="string">&#x27;Bob&#x27;</span>, <span class="number">95.0</span>, <span class="string">&#x27;A&#x27;</span>]]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">sorted</span>(gameresult,key = itemgetter(<span class="number">0</span>,<span class="number">1</span>)) <span class="comment">#按姓名升序，相同按分数升序</span></span><br><span class="line">[[<span class="string">&#x27;Alan&#x27;</span>, <span class="number">86.0</span>, <span class="string">&#x27;C&#x27;</span>], [<span class="string">&#x27;Bob&#x27;</span>, <span class="number">95.0</span>, <span class="string">&#x27;A&#x27;</span>], [<span class="string">&#x27;Mandy&#x27;</span>, <span class="number">83.5</span>, <span class="string">&#x27;A&#x27;</span>], [<span class="string">&#x27;Rob&#x27;</span>, <span class="number">89.3</span>, <span class="string">&#x27;E&#x27;</span>]]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">sorted</span>(gameresult,key = itemgetter(<span class="number">2</span>,<span class="number">0</span>)) <span class="comment">#按等级升序，相同按姓名升序</span></span><br><span class="line">[[<span class="string">&#x27;Bob&#x27;</span>, <span class="number">95.0</span>, <span class="string">&#x27;A&#x27;</span>], [<span class="string">&#x27;Mandy&#x27;</span>, <span class="number">83.5</span>, <span class="string">&#x27;A&#x27;</span>], [<span class="string">&#x27;Alan&#x27;</span>, <span class="number">86.0</span>, <span class="string">&#x27;C&#x27;</span>], [<span class="string">&#x27;Rob&#x27;</span>, <span class="number">89.3</span>, <span class="string">&#x27;E&#x27;</span>]]</span><br></pre></td></tr></table></figure></li>
</ul>
</blockquote>

        </div>

        
            <section class="post-copyright">
                
                    <p class="copyright-item">
                        <span>Author:</span>
                        <span><a href="/about/">WD</a></span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>Permalink:</span>
                        <span><a href="https://did321.gitee.io/2019/02/07/python%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E4%BA%8C/">https://did321.gitee.io/2019/02/07/python%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E4%BA%8C/</a></span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>License:</span>
                        <span>Copyright (c) 2022 <a target="_blank" rel="noopener" href="http://creativecommons.org/licenses/by-nc/4.0/">CC-BY-NC-4.0</a> LICENSE</span>
                    </p>
                
                
                     <p class="copyright-item">
                         <span>Slogan:</span>
                         <span><a href="#">The blog is my giant.</a></span>
                     </p>
                

            </section>
        
        <section class="post-tags">
            <div>
                <span>Tag(s):</span>
                <span class="tag">
                    
                    
                        <a href="/tags/python/"># python</a>
                    
                        <a href="/tags/%E5%88%97%E8%A1%A8/"># 列表</a>
                    
                        <a href="/tags/%E5%85%83%E7%BB%84/"># 元组</a>
                    
                        <a href="/tags/%E5%AD%97%E5%85%B8/"># 字典</a>
                    
                        
                </span>
            </div>
            <div>
                <a href="javascript:window.history.back();">back</a>
                <span>· </span>
                <a href="/">home</a>
            </div>
        </section>
        <section class="post-nav">
            
                <a class="prev" rel="prev" href="/2019/02/08/CMake%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">CMake学习笔记</a>
            
            
        </section>
        <br>
        <br>
    
    <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
    <div id="vcomments"></div>
    <script>
        new Valine({
    el: '#vcomments' ,

    appId: 'JvFy3ebVLo2rUYgHaMweJyXX-MdYXbMMI',
    appKey: 'TCFxfjDAM8UmERPEgYXJmT40',
    serverURLs: 'https://JvFy3ebV.api.lncldglobal.com', 
    placeholder: '----评论区----留下你的评论，作者会定期回复！在昵称处填写QQ号可自动获取邮箱和QQ头像（保护QQ邮箱隐私）',
    enableQQ: true,
    requiredFields: ['nick'],
});
    </script>

    </article>
</div>

        </div>
        <footer id="footer" class="footer">
    <div class="copyright">
        <span>© WD | Powered by <a href="https://hexo.io" target="_blank">Hexo</a> & <a href="https://github.com/Siricee/hexo-theme-Chic" target="_blank">Chic</a></span>
    </div>
</footer>

    </div>
</body>

</html>
